<template>
    <div v-if="showDialog" class="confirm-dialog-overlay" :class="showDialog ? 'showDialogs' : ''">
        <div class="confirm-dialog">
            <h3>恭喜您获得<span style="color: red;">【{{ items.discount_money }}元优惠券】</span> </h3>
            <p>是否使用优惠券继续购买?</p>
            <div class="dialog-buttons">
                <button @click="confirmClose">确定</button>
                <button @click="cancelClose">取消</button>
            </div>
        </div>
    </div>
</template>

<script>
// 发送消息的组件
import { EventBus } from './event-bus.js'

const { ipcRenderer } = require('electron')
import axios from 'axios';

export default {
    data() {
        return {
            url: "http://hj.xiaoyutongxue.com",
            items: {},
            numbers: 0,
            showDialog: false,
            haspay: false,
        }
    },
    created() {
        EventBus.$on('haspayorder', data => {
            this.haspay = true
        })

        ipcRenderer.on('confirm-close', () => {
            if (Number(this.items.discount_money) > 0) {
                this.numbers = this.numbers + 1
                if (this.numbers == 2) {
                    // 关了两次
                    ipcRenderer.send('allow-close')
                }
                if (this.haspay) {
                    // 支付过直接关闭
                    ipcRenderer.send('allow-close')
                } else {
                    this.showDialog = true
                }
                if (this.$route.path == '/orderlist') {
                    // 订单页面直接关闭
                    ipcRenderer.send('allow-close')
                }
            } else {
                ipcRenderer.send('allow-close')
            }
            EventBus.$emit('closewin', JSON.stringify(this.items))
        })
        this.getinfo()
    },
    methods: {
        getinfo() {
            axios.get(this.url + '/api/index/get_info?type_id=' + 1)
                .then(response => {
                    this.items = response.data.data
                })
                .catch(error => {
                    console.error('Error fetching data:', error);
                });
        },
        confirmClose() {
            this.showDialog = false
            // ipcRenderer.send('allow-close')
            ipcRenderer.send('cancel-close')
        },
        cancelClose() {
            this.showDialog = false
            ipcRenderer.send('cancel-close')
        }
    }
}
</script>

<style scoped>
.confirm-dialog-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(0, 0, 0, 0.5);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 9999;
    display: none;
    padding-top: 200px;

}

.showDialogs {
    display: block;
}

.confirm-dialog {
    background-color: white;
    padding: 20px;
    border-radius: 8px;
    width: 300px;
    margin: 0 auto;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

.confirm-dialog h3 {
    margin-top: 0;
    color: #333;
}

.dialog-buttons {
    display: flex;
    justify-content: flex-end;
    margin-top: 20px;
    gap: 10px;
}

.dialog-buttons button {
    padding: 8px 16px;
    border: none;
    border-radius: 4px;
    cursor: pointer;
}

.dialog-buttons button:first-child {
    background-color: #f56c6c;
    color: white;
}

.dialog-buttons button:last-child {
    background-color: #f0f0f0;
    color: #333;
}
</style>